﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using com.eforceglobal.crux.views.Administration;
using com.eforceglobal.crux.BusinessFacades;
using com.eforceglobal.crux.security.EncryptionAndHashing.Enums;
using com.eforceglobal.crux.configuration;
using com.eforceglobal.crux.logging;
using com.eforceglobal.crux.logging.Enums;
using com.eforceglobal.crux.DTOs;
using com.eforceglobal.crux.exceptions;
using com.eforceglobal.crux.security;

namespace com.eforceglobal.crux.controllers.Administration
{
    public class ResetPasswordController
    {
        IResetPassword _view = null;
        public ResetPasswordController(IResetPassword view)
        {
            _view = view;
        }

        public void InitView(bool IsPostBack)
        {
            if (!IsPostBack)
            {
                CheckValidity();
            }
        }

        private void CheckValidity()
        {
            SessionData sessionData = new SessionData();
            UserFacade userFacade = new UserFacade();
            UserInformation currentUser = (UserInformation) sessionData.CurrentUser;
            UserInformation userBeingModified = userFacade.GetUser(_view.UserID);
            ICruxSecurityManager cruxSecurityManager = CruxSecurityManagerFactory.GetSecurityManager();
            if (!cruxSecurityManager.IsUserAuthorizedToChangePassword(currentUser, userBeingModified))
                NavigationController.OpenErrorPage("805");
        }

        public void ResetPassword()
        {
            bool success = false;
            if (PasswordConfirmationOk())
            {
                try
                {
                    SaveNewPassword();
                    success = true;
                }
                catch (Exception ex)
                {
                    ApplicationLogger.LogApplicationMessage(
                      ex.Message, ApplicationLogType.Database, ex.StackTrace);
                    _view.ErrorDetails = CruxApplicationErrorManager.GetApplicationError("101", ex.Message);
                }
            }
            else
            {
                _view.ErrorDetails = CruxApplicationErrorManager.GetApplicationError("802");
            }

            if (success) RedirectToRelevantView();
        }

        public void RedirectToRelevantView()
        {
            UserFacade facade = new UserFacade();
            UserInformation user = facade.GetUser(_view.UserID);
            SessionData sessionData = new SessionData();
            if (_view.UserID == sessionData.CurrentUser.UserID)
                NavigationController.OpenView("Administration");
            else
                NavigationController.OpenView("ManageUsers",user.CompanyID);
             
        }
        private void SaveNewPassword()
        {
            PasswordFacade passwordFacade = new PasswordFacade();
            PasswordStorageType passwordStorageType = CruxConfigurationManager.GetPasswordStorageType();
            passwordFacade.ResetPassword(_view.UserID, _view.NewPassword, passwordStorageType);
        }

        private bool PasswordConfirmationOk()
        {
            if (_view.NewPassword != _view.ConfirmedNewPassword)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}
